﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Media;
using System.Runtime.InteropServices;//播放声音
using LxzhToast;

namespace AlarmClock
{
    public partial class FrmAddMusic : Form
    {
        public FrmAddMusic()
        {
            InitializeComponent();
        }

        public DataBase database;
        private List<Music> musicList;
        private Music addmusic=new Music();
        private Toast toast;

        int count = 0;

        private void FrmAddMusic_Load(object sender, EventArgs e)
        {
            musicList = database.getMusicList();
            count = musicList.Count();
            for (int i = 0; i < count; i++)
            {
                musiclist.Items.Add(musicList.ElementAt(i).name);
            }
            musicnu.Text = count + "";
            if (count == 0 && !File.Exists("Dala.mp3"))
            {
                try
                {
                    File.Copy(@"../../Resources/Dala.mp3", "Dala.mp3");
                }
                catch (Exception)
                {
                }
            }
        }

        private void btnAddMusic_Click(object sender, EventArgs e)
        {
            if (count == 20)
            {
                if (toast != null)
                    toast.Close();
                toast = new Toast(3, "最多可以添加20首铃声");
                toast.Show();
                return;
            }

            OpenFileDialog Opfd = new OpenFileDialog();
            Opfd.Title = "添加铃声";
            Opfd.Filter = "Music Files|*.mp3|All Files|*.*";

            if (DialogResult.OK == Opfd.ShowDialog())
            {
                string filepath = Opfd.FileName.ToString(); 
                string filename=filepath.Substring(filepath.LastIndexOf("\\")+1);
                if (database.isMusicExist(filename))
                {
                    if (toast != null)
                        toast.Close();
                    toast = new Toast(3, "您已添加了该铃声!");
                    toast.Show();
                    return;
                }
                addmusic.name = filename;
                addmusic.path = filepath;
                musicList.Add(addmusic);
                database.addMusic(count,addmusic);
                count++;
                musiclist.Items.Add(addmusic.name);
                this.musicnu.Text = count.ToString();
            }                   
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.musiclist.Items.Clear();
            count = 0;
            this.musicnu.Text = "";
            musicList.Clear();
            database.deleteAllMusic();
        }

        private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int selectedIndex = musiclist.SelectedIndex;
            database.deleteMusic(selectedIndex);
            database.alterAlarm(selectedIndex);
            musiclist.Items.RemoveAt(selectedIndex);
        }

        private void btnME_Click(object sender, EventArgs e)
        {
            if (toast != null)
                toast.Close();
            toast = new Toast(5, "作者：雷进锋\nQQ：1239848066");
            toast.Show();
        }

        private void play(int x)
        {
            if(x>0)
            {

            }
            try
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ": " + ex.StackTrace.ToString(), "Error");
            }
        }

        [DllImport("Winmm.dll")]
        private static extern int waveOutSetVolume(int hwo, System.UInt32 pdwVolume);
        [DllImport("Winmm.dll")]
        private static extern int waveOutGetVolume(int hwo, System.UInt32 pdwVolume);
        //private static extern int waveOutGetVolume(int hwo, out   System.UInt32 pdwVolume); 
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            waveOutSetVolume(0, (System.UInt32)((double)0xffff * (double)this.tBarVoice.Value / (double)(this.tBarVoice.Maximum - this.tBarVoice.Minimum)));
        }
        //控制声音

        private void mediaplayer_Load(object sender, EventArgs e)
        {
            //this.skinEngine.SkinFile = "XPOrange.ssk";
        }
    }
}
